package com.boot.utils;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* FileName: DateUtil.java
* 时间工具类
*
* @author 张志磊
* @date   2019/2/23 9:33
* @version 1.0
*/
public class DateUtil {
    /**
    * @description 获取一天的开始时间
    * @author 张志磊
    * @date 2019/2/23 9:34
    * @param date
    * @return
    */
    public static Date getStartOfADay(Date date){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }
    /**
     * @description 获取一天的结束时间
     * @author 张志磊
     * @date 2019/2/23 9:34
     * @param date
     * @return
     */
    public static Date getEndOfADay(Date date){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        calendar.set(Calendar.MILLISECOND,999);
        return calendar.getTime();
    }
    public static Date backDate(){
        Calendar curr = Calendar.getInstance();
        curr.set(Calendar.DAY_OF_MONTH,curr.get(Calendar.DAY_OF_MONTH)-6);
        curr.set(Calendar.HOUR_OF_DAY,0);
        curr.set(Calendar.MINUTE,0);
        curr.set(Calendar.SECOND,0);
        curr.set(Calendar.MILLISECOND,0);
        Date date=curr.getTime();
        return date;
    }
    public static Date changeDay(Integer day){
        Calendar curr = new GregorianCalendar();
        curr.add(Calendar.DATE,day);
//        curr.set(Calendar.DAY_OF_YEAR,curr.get(Calendar.DAY_OF_YEAR)+day);
        Date date=curr.getTime();
        return date;
    }
    public static Date changeDay(Date date,Integer day){
        Calendar curr = new GregorianCalendar();
        curr.setTime(date);
        curr.add(Calendar.DATE,day);
//        curr.set(Calendar.DAY_OF_YEAR,curr.get(Calendar.DAY_OF_YEAR)+day);
        Date date1=curr.getTime();
        return date1;
    }
    public static Boolean isTheSameDay(Date date1,Date date2){
        Calendar calendar1 = new GregorianCalendar();
        calendar1.setTime(date1);
        Calendar calendar2 = new GregorianCalendar();
        calendar2.setTime(date2);
        return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)&&
                calendar1.get(Calendar.DAY_OF_YEAR) == calendar2.get(Calendar.DAY_OF_YEAR);
    }
    public static Integer getDayOfWeek(Date date){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }
}
